import baseUrl from './url'

const uploadFile = (data) => {
	console.log(data)
  return new Promise((resolve, reject) => {
    uni.showLoading({
      title: '上传中',
    })
    uni.uploadFile({
      url: baseUrl + '/common/uploadFile', //接口地址
      filePath: data.filePath, // 要上传的文件的路径名
      name: 'files',
      formData: { fileType:data.fileType },
      header: {
				'Content-Type': 'multipart/form-data'
      },
      success(response) {
        console.log(response)
        const uploadRes = JSON.parse(response.data)
        if (uploadRes.code === 0) {
          resolve(uploadRes)
        } else if(uploadRes.code === 126) {
          uni.showToast({
            title: '上传文件过大',
            duration: 2000,
            icon: 'none'
          });
          reject(uploadRes)
        } else {
          uni.showToast({
            title: '网络异常',
            icon: 'none'
          });
          reject(uploadRes)
        }
      },
      fail(error) {
        uni.showToast({
          title: '上传失败，请重试',
          icon: 'none'
        });
        reject(error)
      },
      complete() {
        uni.hideLoading()
      }
    })
  })
}
export default uploadFile;